﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.BLL;
using RN.ERP.DTO;

namespace RN.ERP.Web.Material.MaterialProperty
{
    public partial class Info : BasePage
    {
        MaterialBLL bll = new MaterialBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            int iStatus = 0;
            DTO.MateProperty property = new MateProperty()
            {
                cCUid = UserInfo.CorpUserGuid,
                cMemo = txtArememo.Value.ToString(),
                cMPName = txtMPName.Value.ToString(),
                iStatus = iStatus
            };


            DTO.OperationResult result = new OperationResult();
            switch (hdnType.Value)
            {
                case "0":
                    result = bll.AddMaterialProperty(property);
                    break;
                case "2":
                    int mpid = 0;
                    int.TryParse(hdnId.Value.Trim(), out mpid);
                    property.id = mpid;
                    result = bll.EditMaterialProperty(property);
                    break;
                default:
                    break;
            }

            if (result.Result)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "','',CloseWin);", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "');", true);
            }
        }

        private void InitPage()
        {
            string optType = Request.QueryString["type"];
            hdnType.Value = optType;
            switch (optType)
            {
                case "1":
                    InitViewMode(true);
                    break;
                case "2":
                    InitViewMode(false);
                    break;
                default:
                    break;
            }
        }

        private void InitViewMode(bool isLock)
        {
            string mpId = Request.QueryString["mpid"];
            int id = 0;
            if (int.TryParse(mpId, out id))
            {
                hdnId.Value = mpId;
                DTO.MateProperty property = bll.GetMaterialProperty(id);
                txtArememo.Value = property.cMemo;
                txtMPName.Value = property.cMPName;
                if (isLock)
                {
                    //txtAreMemo.Enabled = false;
                    txtMPName.Visible = false;
                    LButSave.Visible = false;
                }
            }
        }
    }
}